<?php

/*
 * This class is used for common same function.
 */

class Model_Lead extends Model_Base {
	
	protected $_name,$_primaryKey;
    
    public function init() {
        $this->_name = $this->LEAD_TBL;
        $this->_primaryKey = 'lead_id';
    }
	
	 
	public function getStatusArray() {
        return array(
            "" => "-- Select Status --",
            "1" => "Active",
            "0" => "Inactive"
        );
    }
	
	public function getLeadcurrencyArray() {
        return array(
            "" => "-- Select Lead Currency --",
            "us_dollar" => "US Dollar",
            "euro" => "EURO"
        );
    }
	
	public function getLeadmainstatusArray() {
        return array(
            "" => "",
            "unassigned" => "Unassigned",
            "responsible_assigned" => "Responsible Assigned",
			"cannot_contact" => "Cannot Contact",
            "in_progress" => "In Progress",
			"on_hold" => "On Hold",
            "restored" => "Restored",
			"junk_lead" => "Junk Lead",
            "waiting_for_details" => "waiting for details"
        );
    }
	
	public function getLeadsourceArray() {
        return array(
            "" => "",
            "personal_contact" => "Personal Contact",
            "existing_client" => "Existing Client",
            "call" => "Call",
			"general" => "Website",
            "e-mail" => "E-Mail",
			"conference" => "Conference",
            "show/exhibition" => "Show/Exhibition",
			"employee" => "Employee",
			"campaign" => "Campaign",
            "hr_department" => "HR Department",
			"e-mail_message" => "E-Mail Message",
            "other" => "Other",
        );
    }
	
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('lead' => $this->LEAD_TBL))
                ->where('lead_id = ?', $id)
				->where("lead_status != 2");
     
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	 public function fetchLeadData($where = "", $limit = "", $offset = "0", $order = "lead_id", $sort = "ASC") {
       $select = $this->select()
                ->from(array("lead" => $this->LEAD_TBL))
				->setIntegrityCheck(false)
				->join(array('lgn' => 'tbl_login'), "lead.lead_responsible = lgn.lgn_id")
				->where("lead_status != 2");
        if ($where != "") {
            $select->where($where);
        }
		//echo $select;exit;
        if ($limit != "") {
            $select->limit($limit, $offset);
        }
        $select->order($order . " " . $sort);
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
	
	/* public function  fetchClient($where = ""){
        
        $select = $this->select()->setIntegrityCheck(false)
                ->from(array("clnt" => $this->CLIENT_TBL))
                ->where("clnt_status != 2");
		if ($where != '') {
            $select->where($where);
        }
		
        $result = $this->fetchAll($select);
		//echo $select;
		
        $finalArray[''] = '';
        foreach ($result as $res) {
            
            $finalArray[$res['clnt_id']] = $res['clnt_first_name']. " " .$res['clnt_middle_name']. " " .$res['clnt_last_name'];
        }
		
        return $finalArray;
		
    }*/
	
	public function getLeadCount($where = "") {
        $select = $this->select()
                ->from(array("clnt" => $this->LEAD_TBL), Array('cnt' => 'count(lead_id)'))
			    ->where("lead_status != 2");
        if ($where != '') {
            $select->where($where);
        }

        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }
	
	public function deleteLead($leadArray) {
        if (is_array($leadArray)) {
            foreach ($leadArray as $_lead) {
                $leadData = Array();
                $leadData = $this->fetchEntryById($_lead);
                if (count($leadData) > 0) {
                    $data = array("lead_status" => "2");
                    $this->update($data, "lead_id =" . (int) $_lead);
                }
            }
        } else {
            $lead_id = $leadArray;
            $leadData = $this->fetchEntryById($lead_id);
            if (count($leadData) > 0) {
                $data = array("lead_status" => "2");
                $this->update($data, "lead_id =" . $lead_id);
            }
        }
    }
}